package com.ssm.controller;

import com.ssm.entity.Score;
import com.ssm.service.ScoreService;
import com.ssm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 成绩管理控制器
 */
@RestController
@RequestMapping("/api/scores")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    /**
     * 获取所有成绩
     * @return 成绩列表
     */
    @GetMapping
    @Cacheable(value = "scoreList", key = "'all'")
    public Result<List<Score>> getAllScores() {
        List<Score> scores = scoreService.findAll();
        return Result.success(scores);
    }

    /**
     * 根据ID获取成绩
     * @param id 成绩ID
     * @return 成绩信息
     */
    @GetMapping("/{id}")
    @Cacheable(value = "score", key = "#id")
    public Result<Score> getScoreById(@PathVariable Integer id) {
        Score score = scoreService.findById(id);
        if (score != null) {
            return Result.success(score);
        } else {
            return Result.fail("成绩不存在");
        }
    }

    /**
     * 添加成绩
     * @param score 成绩信息
     * @return 添加结果
     */
    @PostMapping
    @CacheEvict(value = {"scoreList", "score"}, allEntries = true)
    public Result<Score> addScore(@Valid @RequestBody Score score) {
        boolean success = scoreService.add(score);
        if (success) {
            return Result.success(score);
        } else {
            return Result.fail("添加成绩失败");
        }
    }

    /**
     * 更新成绩
     * @param id 成绩ID
     * @param score 成绩信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @CacheEvict(value = {"scoreList", "score"}, allEntries = true)
    public Result<Score> updateScore(@PathVariable Integer id, @Valid @RequestBody Score score) {
        score.setScoreId(id);
        boolean success = scoreService.update(score);
        if (success) {
            return Result.success(score);
        } else {
            return Result.fail("更新成绩失败");
        }
    }

    /**
     * 删除成绩
     * @param id 成绩ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @CacheEvict(value = {"scoreList", "score"}, allEntries = true)
    public Result<Void> deleteScore(@PathVariable Integer id) {
        boolean success = scoreService.delete(id);
        if (success) {
            return Result.success();
        } else {
            return Result.fail("删除成绩失败");
        }
    }

    /**
     * 根据学生ID查询成绩
     * @param studentId 学生ID
     * @return 成绩列表
     */
    @GetMapping("/student/{studentId}")
    @Cacheable(value = "scoreList", key = "'student:' + #studentId")
    public Result<List<Score>> getScoresByStudentId(@PathVariable Integer studentId) {
        List<Score> scores = scoreService.findByStudentId(studentId);
        return Result.success(scores);
    }

    /**
     * 根据课程ID查询成绩
     * @param courseId 课程ID
     * @return 成绩列表
     */
    @GetMapping("/course/{courseId}")
    @Cacheable(value = "scoreList", key = "'course:' + #courseId")
    public Result<List<Score>> getScoresByCourseId(@PathVariable Integer courseId) {
        List<Score> scores = scoreService.findByCourseId(courseId);
        return Result.success(scores);
    }
} 